﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Text;

public partial class _default : System.Web.UI.Page 
{
    public string sqlstr;
    public string ml,smallmenu,snotice;
	
	public string toptitle,nowdate,nowweek;//头条新闻标题
	public string topcontent,toppictitle;//头条新闻内容
	public string title,picurl;
	public string addtime,newsid="";
    public string liTest = string.Empty;
    public string newsHtml, teamHtml, qaHtml, refHtml, fileHtml, caseXsHtml,caseMsHtml,caseXzHtml,caseQtHtml,onlineHtml,dealHtml;
    protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack)
        {
            //工作动态
            newsHtml = ShowData("3", "12", 46, "newscontent.aspx");
            //团队风采
            teamHtml = ShowData("4", "12", 46, "newscontent.aspx");

            //网上咨询
            onlineHtml = ShowGuestData("0");
            //诉前调解
            dealHtml = ShowDealData("0");

            //以案说法
            caseXsHtml = ShowData("29", "7", 45, "newscontent.aspx");
            caseMsHtml = ShowData("30", "7", 45, "newscontent.aspx");
            caseXzHtml = ShowData("31", "7", 45, "newscontent.aspx");
            caseQtHtml = ShowData("32", "7", 45, "newscontent.aspx");

            //常见问题
            qaHtml = ShowData2("10");
            //诉讼指南
            refHtml = ShowData2("8");
            //诉讼文书
            fileHtml = ShowData2("9");		
         }
    }

    private string ShowData(string smallClassID,string topCount,int titleLength,string page)
    {
        string sql = string.Format("select top {1} id,title,addtime from web_notice where smallclassid={0} order by addtime desc,id desc ",
            smallClassID, topCount);
        Socut.Reader sqlDataReaderjdnew = new Socut.Reader(sql);
        StringBuilder newsBuilder = new StringBuilder();
        while (sqlDataReaderjdnew.Read())
        {
            string news = string.Format("<li><span>{0}</span><a href=\"{3}?ID={1}\" target=\"_blank\">{2}</a></li>",
                DateTime.Parse(sqlDataReaderjdnew["addtime"].ToString()).ToShortDateString(), sqlDataReaderjdnew["id"].ToString(),
                CutString(sqlDataReaderjdnew["title"].ToString(), titleLength),page);
            newsBuilder.Append(news);
        }        
        sqlDataReaderjdnew.Close();

        return newsBuilder.ToString();
    }

    private string ShowData2(string smallClassID)
    {
        string sql = string.Format("select top 10 id,title,addtime from web_notice where smallclassid={0} order by addtime desc,id desc", smallClassID);
        Socut.Reader sqlDataReaderjdnew = new Socut.Reader(sql);
        StringBuilder newsBuilder = new StringBuilder();
        while (sqlDataReaderjdnew.Read())
        {
            string news = string.Format("<li><a href=\"newscontent.aspx?ID={0}\" target=\"_blank\">{1}</a></li>",
                sqlDataReaderjdnew["id"].ToString(), CutString(sqlDataReaderjdnew["title"].ToString(), 38));
            newsBuilder.Append(news);
        }
        sqlDataReaderjdnew.Close();

        return newsBuilder.ToString();
    }

    private string ShowGuestData(string gisshow)
    {
        string sql = string.Format("select top 10 * from web_guestbook where gisshow={0} order by id desc", gisshow);
        Socut.Reader sqlDataReaderjdnew = new Socut.Reader(sql);
        StringBuilder newsBuilder = new StringBuilder();
        
        if (sqlDataReaderjdnew.Read())
        {
            while (sqlDataReaderjdnew.Read())
            {
                newsBuilder.Append("<tr>");
                newsBuilder.Append(string.Format("<td>Z000{0}</td>", sqlDataReaderjdnew["id"].ToString()));
                newsBuilder.Append(string.Format("<td><a href=\"gbookviewcheck.aspx?id={0}\" target=\"_blank\">{1}</a></td>", 
                    sqlDataReaderjdnew["id"].ToString(), CutString(sqlDataReaderjdnew["gtitle"].ToString(), 25)));
                newsBuilder.Append(string.Format("<td>{0}</td>", CutString(sqlDataReaderjdnew["gname"].ToString(), 8)));
                newsBuilder.Append(string.Format("<td>{0}</td>", DateTime.Parse(sqlDataReaderjdnew["gdate"].ToString()).ToShortDateString()));
                newsBuilder.Append(string.Format("<td>{0}</td>", checkstatus(sqlDataReaderjdnew["gcheck"].ToString())));
                newsBuilder.Append("");
            }
        }
        else
        {
            newsBuilder.Append("<tr><td></td>");
            newsBuilder.Append("<td></td>");
            newsBuilder.Append("<td></td>");
            newsBuilder.Append("<td></td></tr>");
        }        
        sqlDataReaderjdnew.Close();

        return newsBuilder.ToString();
    }

    private string ShowDealData(string gisshow)
    {
        string sql = string.Format("select top 10 * from web_guestbook where gisshow={0} order by id desc", gisshow);
        Socut.Reader sqlDataReaderjdnew = new Socut.Reader(sql);
        StringBuilder newsBuilder = new StringBuilder();

        if (sqlDataReaderjdnew.Read())
        {
            while (sqlDataReaderjdnew.Read())
            {
                newsBuilder.Append("<tr>");
                newsBuilder.Append(string.Format("<td>Z000{0}</td>", sqlDataReaderjdnew["id"].ToString()));
                newsBuilder.Append(string.Format("<td><a href=\"gbookviewcheck.aspx?id={0}\" target=\"_blank\">{1}</a></td>", 
                    sqlDataReaderjdnew["id"].ToString(), CutString(sqlDataReaderjdnew["gtitle"].ToString(), 35)));
                newsBuilder.Append(string.Format("<td>{0}</td>", CutString(sqlDataReaderjdnew["gname"].ToString(), 8)));
                newsBuilder.Append(string.Format("<td>{0}</td>", checkstatus(sqlDataReaderjdnew["gcheck"].ToString())));
                newsBuilder.Append("");
            }
        }
        else
        {
            newsBuilder.Append("<tr><td></td>");
            newsBuilder.Append("<td></td>");
            newsBuilder.Append("<td></td>");
            newsBuilder.Append("<td></td></tr>");
        }
        sqlDataReaderjdnew.Close();

        return newsBuilder.ToString();
    }


    /// <summary>
    /// 去除HTML标签
    /// </summary>
    /// <param name="RContent">要去除标签的文本</param>
    /// <returns></returns>
    public string Clear_HTML_Code(string RContent)
    {
        int index1 = RContent.IndexOf("<STYLE>");
        if (index1 >= 0)
        {
            int index2 = RContent.LastIndexOf("</STYLE>");
            if (index2 > index1)
            {
               RContent=RContent.Remove(index1, index2 - index1);
            }
        }
        RContent = Regex.Replace(RContent, "<[^>]+>", "");
        
        return RContent;
    }
    public static string getStr(string s, int l)
    {
        string temp = s;
        temp = temp.Replace("<div>", "");
        temp = temp.Replace("&nbsp;", "");
        temp = temp.Replace("</div>", "");
		
        if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
        {
            return temp;
        }
        for (int i = temp.Length; i >= 0; i--)
        {
            temp = temp.Substring(0, i);
            if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l - 3)
            {
                return temp + "";
            }
        }
        return "";
    }

public string CutString(string str,int length)
        {
            int i = 0, j = 0;
            foreach(char chr in str)
            {
                if((int)chr > 127)
                {
                    i += 2;
                }
                else
                {
                    i ++;
                }
                if (i > length)
                {
                    str = str.Substring(0, j) + "…";
                    break;
                }
                j ++;
            }
            return str;
        } 
    public string htmlstr(string chr)
    {
        if (chr == null)
            return "";
        //chr = chr.Replace("<", "&lt");
        //chr = chr.Replace(">", "&gt");
        chr = chr.Replace("\n", "<br>");
        //chr = chr.Replace("\"", "&quot;");
        //chr = chr.Replace("'", "&#39;");
        chr = chr.Replace(" ", "&nbsp;");
        chr = chr.Replace("select", "");
        chr = chr.Replace("delete", "");
        chr = chr.Replace("update", "");
        chr = chr.Replace("drop", "");
        //chr = chr.Replace(";", "");
        //chr = chr.Replace("(", "");
        //chr = chr.Replace(")", "");
        chr = chr.Replace("and", "");
        chr = chr.Replace("or", "");
        //chr = chr.Replace("\r", "");
        return (chr);
    }

    public string checkstatus(string str)
    {
        string strstatus = "";
        if (str == "0")
        {
            strstatus = "等待回复";
        }
        else if (str == "1")
        {
            strstatus = "已经回复";
        }
        return strstatus;
    }

}
